﻿using System;
using Fruits.Sound;
using GameFramework.Localization;
using UnityGameFramework.Runtime;
using GameEntry = Fruits.Base.GameEntry;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

namespace Fruits.Procedure
{
    public class ProcedureLaunch : ProcedureBase
    {
        public override bool UseNativeDialog => true;

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);

            // 构建信息：发布版本时，把一些数据以 Json 的格式写入 Assets/GameMain/Configs/BuildInfo.txt，供游戏逻辑读取
            GameEntry.BuiltinData.InitBuildInfo();
            
            //  设置语言
            InitLanguageSettings();
            
            //  变体配置
            InitCurrentVariant();
            
            //  声音配置
            InitSoundSettings();
            
            // 默认字典：加载默认字典文件 Assets/GameMain/Configs/DefaultDictionary.xml
            // 此字典文件记录了资源更新前使用的各种语言的字符串，会随 App 一起发布，故不可更新
            GameEntry.BuiltinData.InitDefaultDictionary();
        }

        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);

            //  运行一帧就立即切换到 Splash（过渡，展示游戏健康声明）流程
            ChangeState<ProcedureSplash>(procedureOwner);
        }


        /// <summary>
        /// 语言配置，如果不设置，则使用默认语言
        /// </summary>
        private void InitLanguageSettings()
        {
            if (GameEntry.Base.EditorResourceMode & GameEntry.Base.EditorLanguage != Language.Unspecified)
            {
                //  编辑器资源模式直接使用, Inspector上设置的语言
                return;
            }
            
            Language language = GameEntry.Localization.Language;
            //  如果游戏已经设置了语言
            if (GameEntry.Setting.HasSetting(Constant.Setting.Language))
            {
                try
                {
                    string languageString = GameEntry.Setting.GetString(Constant.Setting.Language);
                    //  使用设置的语言
                    language = (Language)Enum.Parse(typeof(Language), languageString);
                }catch
                {
                    //  忽略错误
                }
            }

            //  检查语言, 如果是不支持的语言，则使用英语
            
            //  这里支持的语言需要做成配置文件读取
            
            if (language != Language.English
                && language != Language.ChineseSimplified
                && language != Language.ChineseTraditional
                && language != Language.Spanish)
            {
                language = Language.English;
                GameEntry.Setting.SetString(Constant.Setting.Language, language.ToString());
                GameEntry.Setting.Save();
            }
            
            //  设置语言
            GameEntry.Localization.Language = language;
            Log.Info("Init language settings complete, current language is '{0}'.", language.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        private void InitCurrentVariant()
        {
            if (GameEntry.Base.EditorResourceMode)
            {
                //  编辑器资源模式不使用AssetBundle，也就不需要变体
                return;
            }

            string currentVariant = null;
            switch (GameEntry.Localization.Language)
            {
                case Language.ChineseSimplified:
                    currentVariant = "zh-cn";
                    break;
                case Language.ChineseTraditional:
                    currentVariant = "zh-tw";
                    break;
                case Language.English:
                    currentVariant = "en-us";
                    break;
                case Language.Spanish:
                    currentVariant = "sp-es";
                    break;
                
                default:
                    currentVariant = "zh-cn";
                    break;
            }
            
            GameEntry.Resource.SetCurrentVariant(currentVariant);
            Log.Info("Init current variant complete.");
        }

        /// <summary>
        /// 声音配置
        /// </summary>
        private void InitSoundSettings()
        {
            GameEntry.Sound.Mute("Music", GameEntry.Setting.GetBool(Constant.Setting.MusicMuted, false));
            GameEntry.Sound.SetVolume("Music", GameEntry.Setting.GetFloat(Constant.Setting.MusicVolume, 0.3f));
            GameEntry.Sound.Mute("Sound", GameEntry.Setting.GetBool(Constant.Setting.SoundMuted, false));
            GameEntry.Sound.SetVolume("Sound", GameEntry.Setting.GetFloat(Constant.Setting.SoundVolume, 1f));
            GameEntry.Sound.Mute("UISound", GameEntry.Setting.GetBool(Constant.Setting.UISoundMuted, false));
            GameEntry.Sound.SetVolume("UISound", GameEntry.Setting.GetFloat(Constant.Setting.UISoundVolume, 1f));
            
        }


    }
}